


package yf.mall.controller;
import com.yf.request.SearchParam;

import java.util.List;

import com.yf.response.RRException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;
import yf.mall.entity.ShopEntity;
import yf.mall.entity.UserEntity;
import yf.mall.service.ShopService;
import yf.mall.service.UserService;
import yf.mall.vo.MaterialVo;
import yf.mall.service.MaterialService;
import com.yf.response.R;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
                                            import yf.mall.enums.MaterialStatusEnum;
    
/**
 * 爆款素材Controller
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Sat Oct 17 14:55:42 CST 2020
 */
@RestController
@RequestMapping("/material")
public class MaterialController {
    @Autowired
    private MaterialService materialService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private UserService userService;

    /**
     * 列表爆款素材
     */
    @SysLog("查看爆款素材")
    @RequestMapping("/list")
    @RequiresPermissions("material:list")
    public R list(@RequestBody SearchParam<MaterialVo> params) {
        PageHelper.startPage(params.getPage(), params.getLimit()).setOrderBy(params.getOrder());
        List<MaterialVo> list = materialService.findVo(params.getEntity());
        return R.ok(new PageInfo(list));
    }

    /**
 * 下载订单
 */
    @SysLog("下载订单")
    @RequestMapping("/down")
    @RequiresPermissions("material:list")
    public void down(@RequestBody SearchParam<MaterialVo> params, HttpServletResponse response) {
        List<MaterialVo> list = materialService.findVo(params.getEntity());
            materialService.downLoad(response, list);
    }


    /**
     * 查看爆款素材
     */
    @SysLog("查看爆款素材")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("material:info")
    public R info(@PathVariable("id") Long id) {
            MaterialVo vo = materialService.getVo(id);

        return R.ok(vo);
    }

    /**
     * 保存爆款素材
     */
    @SysLog("保存爆款素材")
    @RequestMapping("/save")
    @RequiresPermissions("material:save")
    public R save(@RequestBody MaterialVo vo) {
        if (vo.getMaterial().getUserCode() != null && !vo.getMaterial().getUserCode().equals("")){
            UserEntity userEntity = userService.findByCode(vo.getMaterial().getUserCode());
            if (userEntity == null){
                throw new RRException("用户编号不存在");
            }
            vo.getMaterial().setUserId(userEntity.getId());
        }
            materialService.saveVo(vo);
        ShopEntity shopEntity = shopService.get(vo.getMaterial().getShopId());
        vo.getMaterial().setShopEntity(shopEntity);
        return R.ok(vo);
    }

    /**
     * 修改爆款素材
     */
    @SysLog("修改爆款素材")
    @RequestMapping("/update")
    @RequiresPermissions("material:update")
    public R update(@RequestBody MaterialVo vo) {
        if (vo.getMaterial().getUserCode() != null && !vo.getMaterial().getUserCode().equals("")){
            UserEntity userEntity = userService.findByCode(vo.getMaterial().getUserCode());
            if (userEntity == null){
                throw new RRException("用户编号不存在");
            }
            vo.getMaterial().setUserId(userEntity.getId());
        }
            materialService.updateVo(vo);

        return R.ok(vo);
    }

    /**
     * 删除爆款素材
     */
    @SysLog("删除爆款素材")
    @RequestMapping("/delete")
    @RequiresPermissions("material:delete")
    public R delete(Long[]ids) {
            materialService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 所有爆款素材
     */
    @SysLog("所有爆款素材")
    @RequestMapping("/all")
    public R all(String order) {
        PageHelper.orderBy(order!=null ? order : "id desc");
        List<MaterialVo> list = materialService.findVo(null);
        return R.ok(list);
    }

    /**
     * 所有enums
     */
    @SysLog("所有enums")
    @RequestMapping("/dmz/options")
    public R options() {
        HashMap<String,Object> options = new HashMap();
                                                                                                                                                                                                    options.put("statusOptions",MaterialStatusEnum.names());
                            return R.ok(options);
    }





    /***********自定义开始***********/
}
